package com.caffeinatedbliss.nanowrimo;

import org.nanowrimo.ws.domain.WordCountListEntry;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Paul Hawke
 * Date: Jun 23, 2006
 * Time: 9:36:26 PM
 * To change this template use File | Settings | File Templates.
 */
public class NanoWebserviceUtils {
  public static int[] convertToAmountWritten(WordCountListEntry[] entries) {
    int[] amounts = new int[30];
    int previous = 0;
    GregorianCalendar gcal = new GregorianCalendar();
    for (int i = 0; entries != null && i < entries.length; i++) {
      gcal.setTime(entries[i].getDate());
      amounts[gcal.get(Calendar.DAY_OF_MONTH) - 1] = entries[i].getWordcount() - previous;
      previous = entries[i].getWordcount();
    }

    int found = 0;
    for (int i = 29; i >= 0; i--) {
      if (amounts[i] > 0) {
        found = i;
        break;
      }
    }

    int[] returnAmount = new int[found + 1];
    System.arraycopy(amounts, 0, returnAmount, 0, found + 1);
    return returnAmount;
  }

  public static String getFilename(HttpServletRequest request) {
    String name = "";
    String pathInfo = request.getPathInfo();
    if (pathInfo != null) {
      File file = new File(pathInfo);
      name = file.getName();
    }
    return name;
  }
}
